rm(list = ls())
rawdata<-read.csv("C:/Users/Mittal/Desktop/Logistic_Regression_Raw_Data.csv",header=FALSE)
#library(Matrix);rawdata<-readMM("C:/Users/Mittal/Desktop/NTDBNEW_Imputation_Train_LD_imputed_0_MM.txt")

logRegress<-function(mydata){
	numVar<<-NCOL(mydata)
	Variables<<-names(mydata)[2:numVar]
	browser()
	Y<-mydata[,1]
	d<-mydata[,2:length(mydata[1,])]
	X<-scale(d)

	X.svd<-svd(X)
	Q<-X.svd$v
	P<-X.svd$u
	Z<-P%*%t(Q)

	Z.stand<-scale(Z)

	Lambda<-solve(t(Z.stand)%*%Z.stand)%*%t(Z.stand)%*%X #Obtaining Lambda from equation 7 from Johnson (2000) pg 8

	logrfit<-glm(Y~Z.stand,family=binomial)
	unstCoefs<-coef(logrfit)
	b<-unstCoefs[2:length(unstCoefs)]
	LpredY<-predict(logrfit,newdata=mydata,type="response")
	lYhat<-log(LpredY/(1-LpredY))#Creating logit-Y-hat
	stdlYhat<-sd(lYhat)#Getting stdev of logit-Y-hat
	getting.Rsq<-lm(LpredY~Y)#Getting R-sq
	Rsq<-summary(getting.Rsq)$r.squared
	beta<-b*((sqrt(Rsq))/stdlYhat)#Computing standardized logistic regression coefficients

	epsilon<-Lambda^2%*%beta^2
	R.sq<<-sum(epsilon)
	PropWeights<-(epsilon/R.sq)
	result<<-data.frame(Variables, Raw.RelWeight=epsilon, Rescaled.RelWeight=PropWeights) 
}

logRegress(rawdata)
